#!/bin/sh

#JDK path 
JAVA_HOME=/usr/local/jdk1.6.0_29
echo $JAVA_HOME

#app running user
RUNNING_USER=root

#app language
export LC_ALL=zh_CN.UTF-8

#app home
APP_HOME=/usr/local/kkserver

#app main class
APP_MAINCLASS=net.yhte.its.service.CommService

#build full classpath
CLASSPATH=$APP_HOME/bin
for i in "$APP_HOME"/lib/*.*; do
CLASSPATH="$CLASSPATH":"$i"
done

#JVM running options
JAVA_OPTS="-ms512m -mx512m "
#remote  jconsole config (modifty ipAddress and port)
JMXREMOTE_OPTS=" -Djava.rmi.server.hostname=192.168.241.128 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=911 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
###################################
# check app running's id
################################### 
# initialize the global  psid
psid=0

checkpid() { 
check_cmd=`ps -ef | grep $APP_HOME/ | grep -v grep` 
if [ -n "$check_cmd" ]; then 
psid=`echo $check_cmd | awk '{print $2}'` 
else 
psid=0 
fi 
} 

################################### 
# app start function
################################### 
start() { 
checkpid 

if [ $psid -ne 0 ]; then 
echo "================================" 
echo "warn: $APP_HOME already started! (pid=$psid)" 
echo "================================" 
else 
echo -n "Starting $APP_HOME ..." 
cd $APP_HOME
$JAVA_HOME/bin/java $JAVA_OPTS $JMXREMOTE_OPTS -cp $CLASSPATH $APP_MAINCLASS &
checkpid 
if [ $psid -ne 0 ]; then 
echo "(pid=$psid) [OK]" 
else 
echo "[Failed]" 
fi 
fi 
} 

################################### 
# app stop function
################################### 
stop() {
checkpid

if [ $psid -ne 0 ]; then
echo -n "Stopping $APP_HOME ...(pid=$psid) "
su - $RUNNING_USER -c "kill -9 $psid"
if [ $? -eq 0 ]; then
echo "[OK]"
else
echo "[Failed]"
fi

checkpid
if [ $psid -ne 0 ]; then
stop
fi
else
echo "================================"
echo "warn: $APP_HOME is not running"
echo "================================"
fi
}

################################### 
# app check status function
################################### 
status() { 
checkpid 

if [ $psid -ne 0 ]; then 
echo "$APP_HOME is running! (pid=$psid)" 
else 
echo "$APP_HOME is not running" 
fi 
} 

################################### 
#print system and app options function
################################### 
info() { 
echo "System Information:" 
echo "****************************" 
echo `head -n 1 /etc/issue` 
echo `uname -a` 
echo 
echo "JAVA_HOME=$JAVA_HOME" 
echo `$JAVA_HOME/bin/java -version` 
echo 
echo "APP_HOME=$APP_HOME" 
echo "APP_MAINCLASS=$APP_MAINCLASS" 
echo "****************************" 
} 

################################### 
#
#{start|stop|restart|status|info} 
#
################################### 
case "$1" in 
'start') 
start 
;; 
'stop') 
stop 
;; 
'restart') 
stop 
start 
;; 
'status') 
status 
;; 
'info') 
info 
;; 
*) 
echo "Usage: $0 {start|stop|restart|status|info}" 
exit 1 
esac 
exit 0

